package com.rc.saas.tenant.service.tenant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.common.redis.RedisLoader;
import com.rc.saas.tenant.mapper.order.OrderInfoMapper;
import com.rc.saas.tenant.search.StatisticsSearch;
import com.rc.saas.tenant.service.elec.ElecGunService;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.vo.elec.ChargeLeaderboard;
import com.rc.saas.tenant.vo.elec.ElecPileStatusCountVo;
import com.rc.saas.tenant.vo.elec.StationAllDataVo;
import com.rc.saas.tenant.vo.order.OrderAllDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ConsoleServiceImpl implements ConsoleService {
    @Autowired
    private RedisLoader redisLoader;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private ElecGunService elecGunService;

    @Override
    public OrderAllDataVo countAllOrder(StatisticsSearch search) {

//        StringBuffer key = new StringBuffer("statistics:").append(search.getTenantCode());
//        if (search.getEnterpriseId() != null) {
//            key.append(":enterprise:").append(search.getEnterpriseId());
//        }
//        if (search.getOperatorId() != null) {
//            key.append(":operator:").append(search.getOperatorId());
//        }
//        key.append(":all");
//        String result = (String)redisLoader.get(key.toString());
//        if (result != null) {
//            OrderAllDataVo data = JSONObject.parseObject(result, OrderAllDataVo.class);
//            return data;
//        }

        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime todayDate = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);//当天零点
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endTime = dateTimeFormatter.format(todayDate);
        search.setStartTime(null);
        search.setEndTime(endTime);
        OrderAllDataVo data = orderInfoMapper.countAllOrder(search);
        if (data == null || data.getChargingTimeCount() == null) {
            return new OrderAllDataVo();
        }

        //秒转小时
        data.setChargingTimeCount(data.getChargingTimeCount().divide(new BigDecimal("3600"), 1, BigDecimal.ROUND_HALF_UP));

//        LocalDateTime tomorrowDate = localDateTime.plusDays(1);
//        tomorrowDate = LocalDateTime.of(tomorrowDate.toLocalDate(), LocalTime.MIN);//当天零点
//        Duration duration = Duration.between(localDateTime, tomorrowDate);
//        long remain = duration.toMillis();
//        redisLoader.set(key.toString(), JSONObject.toJSONString(data), remain);
        return data;
    }

    @Override
    public OrderAllDataVo countTodayOrder(StatisticsSearch search) {

//        StringBuffer key = new StringBuffer("statistics:").append(search.getTenantCode());
//        if (search.getEnterpriseId() != null) {
//            key.append(":enterprise:").append(search.getEnterpriseId());
//        }
//        if (search.getOperatorId() != null) {
//            key.append(":operator:").append(search.getOperatorId());
//        }
//        key.append(":today");
//        String result = (String)redisLoader.get(key.toString());
//        if (result != null) {
//            OrderAllDataVo data = JSONObject.parseObject(result, OrderAllDataVo.class);
//            return data;
//        }

        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime todayStartDate = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);//当天零点
        LocalDateTime todayEndDate = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);//当天24点
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = dateTimeFormatter.format(todayStartDate);
        String endTime = dateTimeFormatter.format(todayEndDate);
        search.setStartTime(startTime);
        search.setEndTime(endTime);
        OrderAllDataVo data = orderInfoMapper.countAllOrder(search);
        if (data == null || data.getChargingTimeCount() == null) {
            return new OrderAllDataVo();
        }

        //秒转小时
        data.setChargingTimeCount(data.getChargingTimeCount().divide(new BigDecimal("3600"), 1, BigDecimal.ROUND_HALF_UP));

//        LocalDateTime tomorrowDate = localDateTime.plusDays(1);
//        tomorrowDate = LocalDateTime.of(tomorrowDate.toLocalDate(), LocalTime.MIN);//当天零点
//        Duration duration = Duration.between(localDateTime, tomorrowDate);
//        long remain = duration.toMillis();
//        redisLoader.set(key.toString(), JSONObject.toJSONString(data), remain);
        return data;
    }

    @Override
    public StationAllDataVo countStation(StatisticsSearch search) {
//        StringBuffer key = new StringBuffer("statistics:").append(search.getTenantCode()).append(":station_").append(search.getBizType());
//        String result = (String)redisLoader.get(key.toString());
//        if (result != null) {
//            StationAllDataVo data = JSONObject.parseObject(result, StationAllDataVo.class);
//            return data;
//        }

        int stationCount = elecStationService.countStation(search);
        StationAllDataVo data = elecPileService.countPileInConsole(search);
        data.setStationCount(stationCount);

        if (data == null) {
            return new StationAllDataVo();
        }

//        LocalDateTime localDateTime = LocalDateTime.now();
//        LocalDateTime tomorrowDate = localDateTime.plusDays(1);
//        tomorrowDate = LocalDateTime.of(tomorrowDate.toLocalDate(), LocalTime.MIN);//当天零点
//        Duration duration = Duration.between(localDateTime, tomorrowDate);
//        long remain = duration.toMillis();
//        redisLoader.set(key.toString(), JSONObject.toJSONString(data), remain);
        return data;
    }

    @Override
    public ElecPileStatusCountVo countGun(StatisticsSearch search) {
//        StringBuffer key = new StringBuffer("statistics:").append(search.getTenantCode()).append(":pile_").append(search.getBizType());
//        String result = (String)redisLoader.get(key.toString());
//        if (result != null) {
//            ElecPileStatusCountVo data = JSONObject.parseObject(result, ElecPileStatusCountVo.class);
//            return data;
//        }

        ElecPileStatusCountVo data = elecGunService.countGun(search);
        if (data == null) {
            return new ElecPileStatusCountVo();
        }

//        LocalDateTime localDateTime = LocalDateTime.now();
//        LocalDateTime tomorrowDate = localDateTime.plusDays(1);
//        tomorrowDate = LocalDateTime.of(tomorrowDate.toLocalDate(), LocalTime.MIN);//当天零点
//        Duration duration = Duration.between(localDateTime, tomorrowDate);
//        long remain = duration.toMillis();
//        redisLoader.set(key.toString(), JSONObject.toJSONString(data), remain);
        return data;
    }

    @Override
    public List countStationOrderRank(StatisticsSearch search) {

//        StringBuffer key = new StringBuffer("statistics:").append(search.getTenantCode());
//        if (search.getEnterpriseId() != null) {
//            key.append(":enterprise:").append(search.getEnterpriseId());
//        }
//        if (search.getOperatorId() != null) {
//            key.append(":operator:").append(search.getOperatorId());
//        }
//        key.append(":orderRank");
//        if (search.getDateType() != null) {
//            key.append("_").append(search.getDateType());
//        }
//
//        String result = (String)redisLoader.get(key.toString());
//        if (result != null) {
//            List<ChargeLeaderboard> data = JSONArray.parseArray(result, ChargeLeaderboard.class);
//            return data;
//        }

        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = null;

        if ("0".equals(search.getDateType())) {//昨日
            LocalDateTime yesterdayDate = localDateTime.minusDays(1);
            yesterdayDate = LocalDateTime.of(yesterdayDate.toLocalDate(), LocalTime.MIN);//昨日零点

            startTime = dateTimeFormatter.format(yesterdayDate);

        } else if ("1".equals(search.getDateType())) {//当月
            LocalDateTime monthFirstDay = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
            startTime = dateTimeFormatter.format(monthFirstDay);
        } else if ("2".equals(search.getDateType())) {//当年
            LocalDateTime yearFirstDay = localDateTime.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
            startTime = dateTimeFormatter.format(yearFirstDay);
        }

        LocalDateTime todayDate = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);//当天零点
        String endTime = dateTimeFormatter.format(todayDate);

        search.setStartTime(startTime);
        search.setEndTime(endTime);
        List<ChargeLeaderboard> data = orderInfoMapper.countStationOrderRank(search);
        if (data == null) {
            return new ArrayList<ChargeLeaderboard>();
        }

//        LocalDateTime tomorrowDate = localDateTime.plusDays(1);
//        tomorrowDate = LocalDateTime.of(tomorrowDate.toLocalDate(), LocalTime.MIN);//当天零点
//        Duration duration = Duration.between(localDateTime, tomorrowDate);
//        long remain = duration.toMillis();
//        redisLoader.set(key.toString(), JSONObject.toJSONString(data), remain);
        return data;
    }
}
